<?php
/**
 * Created by PhpStorm.
 * User: 丶晓
 * Date: 2017/11/21
 * Time: 8:14
 */

namespace app\api\controller;


use think\Log;
use think\Request;
use app\api\common\Tools;
use app\api\model\DataModel;
use app\api\service\SocketService;
use think\Session;

class Socket
{
    public function __construct()
    {
        if (empty($_POST)) {
            return Tools::error('Think for your Watch!');
        }
    }

    /**
     * 获取Socket Token
     * @Xiao
     * @param $_POST ['miniapp_id']
     */
    public function token(Request $request)
    {
        $Appid = DataModel::selectInfo('appid_miniID', ['id' => $request->post('miniapp_id')]);
        if (!empty($Appid)) {
            return Tools::success('request success!', ['token' => SocketService::getToken($Appid)]);
        }
        return Tools::error('request fail!');
    }

    /**
     * 获取主动推送信息到SocketClient
     * @Xiao
     * @param $_POST ['miniapp_id']
     * @param $_POST ['token']
     * @param $_POST ['messages']
     */
    public function sendToClient(Request $request)
    {
        if (!$request->post('miniapp_id') or !$request->post('token') or !$request->post('messages')) {
            return Tools::error('request lost!');
        }
        $Appid = DataModel::selectInfo('appid_miniID', ['id' => $request->post('miniapp_id')]);
        if (!empty($Appid) and SocketService::vailToken($Appid, $request->post('token'))) {
            $client_id = DataModel::selectInfo('get_client_id', ['id' => $request->post('miniapp_id')]);
            if ($client_id) {
                if (SocketService::sendMessage($client_id, $request->post('messages'))) {
                    return Tools::success('Send Success!');
                }
                return Tools::error('Send fail!');
            }
            return Tools::error('The client not online!');
        }
        return Tools::error('request lost!');
    }

    /*
     * 忽略以下方法，为Socket回调调用方法
     */
    public function clientConnect()
    {
        if (isset($_POST['client_id']) and isset($_POST['client_ip']) and DataModel::changeInfo('miniapp_socket_id', ['ip' => $_POST['client_id'], 'id' => $_POST['client_ip']])) {
            Log::error('ClientID:' . $_POST['client_id'] . 'login. \n' . 'IP:' . $_POST['client_ip']);
        }
        return 'Think for your Watch!';
    }

    /*
     * 忽略以下方法，为Socket回调调用方法
     */
    public function clientMessage()
    {
        // 上线认证
        if (substr($_POST['client_message'], 0, 7) == '@online') {
            $auth = json_decode(substr($_POST['client_message'], 7), true);
            if (!empty($auth) and isset($auth['miniapp_id']) and isset($auth['token'])) {
                $appid = DataModel::selectInfo('authorizer_appid', ['id' => $auth['miniapp_id']]);
                if (SocketService::vailToken($appid, $auth['token'])) {
                    DataModel::changeInfo('miniapp_socket_id_ip', ['miniapp_id' => $auth['miniapp_id'], 'id' => $_POST['client_id'], 'ip' => Request::instance()->ip()]);
                   return "@login";
                }
                return '@auth fail';
            } // @online{"token":"256183a56614049ae0a62886647e606a","miniapp_id":"151"}
            return '@data lost';
        }
        return 'Your send message:'.$_POST['client_message'];
    }

    /*
     * 忽略以下方法，为Socket回调调用方法
     */
    public function clientClose()
    {
        DataModel::deleteInfo('miniapp_socket_offline', ['client_id' => $_POST['client_id']]);
        return 'Think for your Watch!';
    }

}